#include "ViewportControl.h"



ViewportControl::ViewportControl (Ptr< QRect > pCanvasRegion)
: _pViewport (new Viewport), _pCanvasRegion (pCanvasRegion)
{

}



ViewportControl::~ViewportControl ()
{

}



void ViewportControl::viewportMoveTo (QPoint newTranslate)
{
  _pViewport->setTranslate (newTranslate);
  emit repaintViewport ();
}



void ViewportControl::viewportResizeTo (QSize newSize)
{
  _pViewport->setSize (newSize);
  emit repaintViewport ();
}



QRect ViewportControl::getPaintRegion () const
{
  QRect canvasRegion (*_pCanvasRegion);
  QRect viewRegion (QPoint (0, 0), _pViewport->getSize ());
  canvasRegion.translate (viewRegion.center () - _pViewport->getTranslate () 
    - canvasRegion.center ());
  return canvasRegion & viewRegion;
}



QRect ViewportControl::getImageRegion () const
{
  QRect canvasRegion (*_pCanvasRegion);
  QRect viewRegion (QPoint (0, 0), _pViewport->getSize ());
  viewRegion.translate (canvasRegion.center () + _pViewport->getTranslate () 
    -viewRegion.center ());
  return canvasRegion & viewRegion;
}



QRect ViewportControl::getCanvasRegionInViewport () const
{
  QRect canvasRegion (*_pCanvasRegion);
  QRect viewRegion (QPoint (0, 0), _pViewport->getSize ());
  canvasRegion.translate (viewRegion.center () - _pViewport->getTranslate () 
    - canvasRegion.center ());
  return canvasRegion;
}



QPoint ViewportControl::viewportToImage (const QPoint & pointInViewport) const
{
  QRect canvasRegion (*_pCanvasRegion);
  QRect viewRegion (QPoint (0, 0), _pViewport->getSize ());
  return canvasRegion.center () + _pViewport->getTranslate () 
    - viewRegion.center () + pointInViewport;
}



QPoint ViewportControl::imageToViewport (const QPoint & pointInImage) const
{
  QRect canvasRegion (*_pCanvasRegion);
  QRect viewRegion (QPoint (0, 0), _pViewport->getSize ());
  return viewRegion.center () - _pViewport->getTranslate () 
    - canvasRegion.center () + pointInImage;
}


